package com.ghck.server.dao.provider;

import java.text.MessageFormat;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import com.ghck.server.model.ChatThreadUsers;
import com.ghck.server.model.query.ChatThreadUsersQuery;
/**
 * 聊天组成员表  <br>
 * chat_thread_users <br>
 * v1.0 2016-05-01
 * @see com.ajk.server.dao.ChatThreadUsersMapper
 */
 public class ChatThreadUsersProvider {
 
 	/**
 	* 新增 聊天组成员表
 	*/
 	public String insert(ChatThreadUsers domain){
		StringBuilder sb = new StringBuilder();
		
		sb.append("insert into chat_thread_users(create_time,last_modify_time");
	 	if(domain.getThreadId()!=null){
			sb.append(",thread_id");
		}
	 	if(domain.getThreadName()!=null){
			sb.append(",thread_name");
		}
	 	if(domain.getUserId()!=null){
			sb.append(",user_id");
		}
	 	if(domain.getUserName()!=null){
			sb.append(",user_name");
		}
	 	if(domain.getUserRole()!=null){
			sb.append(",user_role");
		}
	 	if(domain.getStartMsgId()!=null){
			sb.append(",start_msg_id");
		}
	 	if(domain.getLastReadMsgId()!=null){
			sb.append(",last_read_msg_id");
		}
	 	if(domain.getLastMsgId()!=null){
			sb.append(",last_msg_id");
		}
	 	if(domain.getUnread()!=null){
			sb.append(",unread");
		}
	 	if(domain.getSysUnread()!=null){
			sb.append(",sys_unread");
		}
	 	if(domain.getSendCount()!=null){
			sb.append(",send_count");
		}
	 	if(domain.getRecCount()!=null){
			sb.append(",rec_count");
		}
	 	if(domain.getIsDelete()!=null){
			sb.append(",is_delete");
		}
	 	if(domain.getIsPush()!=null){
			sb.append(",is_push");
		}
		
		sb.append(") values ( now(),now() ");
		
	 	if(domain.getThreadId()!=null){
			sb.append(",#{threadId}");
		}
	 	if(domain.getThreadName()!=null){
			sb.append(",#{threadName}");
		}
	 	if(domain.getUserId()!=null){
			sb.append(",#{userId}");
		}
	 	if(domain.getUserName()!=null){
			sb.append(",#{userName}");
		}
	 	if(domain.getUserRole()!=null){
			sb.append(",#{userRole}");
		}
	 	if(domain.getStartMsgId()!=null){
			sb.append(",#{startMsgId}");
		}
	 	if(domain.getLastReadMsgId()!=null){
			sb.append(",#{lastReadMsgId}");
		}
	 	if(domain.getLastMsgId()!=null){
			sb.append(",#{lastMsgId}");
		}
	 	if(domain.getUnread()!=null){
			sb.append(",#{unread}");
		}
	 	if(domain.getSysUnread()!=null){
			sb.append(",#{sysUnread}");
		}
	 	if(domain.getSendCount()!=null){
			sb.append(",#{sendCount}");
		}
	 	if(domain.getRecCount()!=null){
			sb.append(",#{recCount}");
		}
	 	if(domain.getIsDelete()!=null){
			sb.append(",#{isDelete}");
		}
	 	if(domain.getIsPush()!=null){
			sb.append(",#{isPush}");
		}
		sb.append(")");
		
		return sb.toString();
	}

	/**
 	* 更新 聊天组成员表
 	*/
	public String update(ChatThreadUsers domain){
		StringBuilder sb = new StringBuilder();
		
		sb.append("update chat_thread_users set last_modify_time = now() ");
		
		if(domain.getThreadId()!=null){
			sb.append(",thread_id=#{threadId}");
		}
		if(domain.getThreadName()!=null){
			sb.append(",thread_name=#{threadName}");
		}
		if(domain.getUserId()!=null){
			sb.append(",user_id=#{userId}");
		}
		if(domain.getUserName()!=null){
			sb.append(",user_name=#{userName}");
		}
		if(domain.getUserRole()!=null){
			sb.append(",user_role=#{userRole}");
		}
		if(domain.getStartMsgId()!=null){
			sb.append(",start_msg_id=#{startMsgId}");
		}
		if(domain.getLastReadMsgId()!=null){
			sb.append(",last_read_msg_id=#{lastReadMsgId}");
		}
		if(domain.getLastMsgId()!=null){
			sb.append(",last_msg_id=#{lastMsgId}");
		}
		if(domain.getUnread()!=null){
			sb.append(",unread=#{unread}");
		}
		if(domain.getSysUnread()!=null){
			sb.append(",sys_unread=#{sysUnread}");
		}
		if(domain.getSendCount()!=null){
			sb.append(",send_count=#{sendCount}");
		}
		if(domain.getRecCount()!=null){
			sb.append(",rec_count=#{recCount}");
		}
		if(domain.getIsDelete()!=null){
			sb.append(",is_delete=#{isDelete}");
		}
		if(domain.getIsPush()!=null){
			sb.append(",is_push=#{isPush}");
		}
		
		sb.append(" where id=#{id}");
		
		return sb.toString();
	}
	
	public String batchInsert(Map<String, List<ChatThreadUsers>> map){
		StringBuffer sb = new StringBuffer();
		List<ChatThreadUsers> chatThreadUserss = map.get("list");
		sb.append("insert into chat_thread_users(create_time,last_modify_time,thread_id,thread_name,user_id,user_name,user_role,start_msg_id,last_read_msg_id,last_msg_id,unread,sys_unread,send_count,rec_count,is_delete,is_push) values");
	    MessageFormat messageFormat = new MessageFormat("("
	    		+ "now(),now()"
	    		+ ",#'{'list[{0}].threadId}"
	    		+ ",#'{'list[{0}].threadName}"
	    		+ ",#'{'list[{0}].userId}"
	    		+ ",#'{'list[{0}].userName}"
	    		+ ",#'{'list[{0}].userRole}"
	    		+ ",#'{'list[{0}].startMsgId}"
	    		+ ",#'{'list[{0}].lastReadMsgId}"
	    		+ ",#'{'list[{0}].lastMsgId}"
	    		+ ",#'{'list[{0}].unread}"
	    		+ ",#'{'list[{0}].sysUnread}"
	    		+ ",#'{'list[{0}].sendCount}"
	    		+ ",#'{'list[{0}].recCount}"
	    		+ ",#'{'list[{0}].isDelete}"
	    		+ ",#'{'list[{0}].isPush}"
	    		+")");
	    		
	    if(CollectionUtils.isNotEmpty(chatThreadUserss)) {
			sb.append(messageFormat.format(new Integer[]{0}));
			for (int i = 1; i <  chatThreadUserss.size(); i++) {
				sb.append(",").append(messageFormat.format(new Integer[]{i}));
			}
		}
		return sb.toString();
	}
	
	/**
 	*通过query 查询 聊天组成员表
 	*/
	public String findByQuery(ChatThreadUsersQuery query){
		StringBuffer sb = new StringBuffer();
		sb.append("select * from chat_thread_users a ");
		
		//TODO: add your logic
		
		if(StringUtils.isEmpty(query.getSortStatment())){
			sb.append(" order by create_time desc ");
		}else{
			sb.append(" ").append(query.getSortStatment());
		}
		if(query.getStart()!=null && query.getLimit()!=null && query.getLimit()!=0){
			sb.append(" limit #{start},#{limit}");
		}else if(query.getLimit()!=null && query.getLimit()!=0){
			sb.append(" limit #{limit}");
		}
		return sb.toString();
	}
	
	public String countByQuery(ChatThreadUsersQuery query){
		StringBuffer sb = new StringBuffer();
		sb.append("select count(1) from chat_thread_users a ");
		return sb.toString();
	}
	
	/**
 	*通过ids 查询 聊天组成员表
 	*/
 	public String findByIds(Map<String, List<Integer>> map){
		StringBuffer sb = new StringBuffer();
		List<Integer> ids = map.get("list");
		sb.append("select * from chat_thread_users where ");
		if(ids==null || ids.isEmpty()){
			sb.append(" 1<2");
		}else{
			if(ids.size()==1){
				sb.append(" id=").append("#{list[0]}");
			}else{
				sb.append(" id in (").append("#{list[0]}");
				for(int i=1; i<ids.size() ; i++){
					sb.append(",").append("#{list[").append(i).append("]}");
				}
				sb.append(")");
			}
		}
		return sb.toString();
	}
	
 }	